<template>
  <el-dialog v-model="dialogVisible"
             :width="width"
             :center="center"
             :title="title"
             append-to-body>
    <el-input type="textarea" v-model="text" rows="6"></el-input>
    <div class="text-right margin-top">
      <el-button @click="applyCommentAndSave(text)">使用并保存</el-button>
      <el-button @click="applyComment(text)">使用</el-button>
    </div>

    <el-table :data="dataSource">
      <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
      <el-table-column label="审批意见" header-align="center" prop="commentText"></el-table-column>
      <el-table-column label="操作" align="center" width="160">
        <template #default="scope">
          <el-button text type="danger" @click="deleteUserComment(scope.row)">删除</el-button>
          <el-button text type="primary" @click="applyComment(scope.row.commentText)">使用</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script>

import myActCommentApi from "@/api/start/activiti/core/myActCommentApi";


export default {
  name: "actHandlerTaskDialog",

  props: {
    width: {
      type: [Number, String],
      default: "60%",
    },
    center: {
      type: [Boolean],
      default: true
    },
    title: {
      type: String,
      default: "审批意见"
    },
    required: {
      type: [Boolean],
      default: true,
    },
    onsubmit: Function
  },
  data() {
    return {
      dialogVisible: true,
      dataSource: [],
      page: {size: 10, current: 1, total: 0},
      text: null,
    }
  },
  created() {
    this.loadUserComment();
  },
  methods: {
    show() {
      this.loadUserComment();
    },
    loadUserComment() {

      myActCommentApi.page({
        size: this.page.size,
        current: this.page.current,
      }).then(res => {
        this.dataSource = res.data.data.records;
        this.page.total = res.data.data.total

      })
    },
    deleteUserComment(row) {
      this.$confirm("您确定要删除吗?").then(() => {
        return myActCommentApi.deleteById(row.id)
      }).then(res => {
        this.$message.success("操作成功")
      }).catch(err => {

      })
    },
    applyCommentAndSave(text) {
      if (!text || text.length < 1) {
        this.$message.warning("请输入审批意见");
        return
      }
      let data = {}
      data.commentText = text;
      myActCommentApi.add(data).then(res => {
        if (this.onsubmit) {
          this.onsubmit(text, this.close)
        }
      }).catch(err => {

      })
    },
    applyComment(text) {
      if (this.required) {
        if (!text || text.length < 1) {
          this.$message.warning("请输入审批意见");
          return
        }
      }

      if (this.onsubmit) {
        this.onsubmit(text, this.close)
      }
    },
    close() {
      this.dialogVisible = false;
    },
    beforeClose() {
      this.dialogVisible = false;
    }
  }
}
</script>

<style scoped>

</style>